/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.insurance.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.insurance.domain.InsuranceScheme;
import me.zhengjie.modules.insurance.service.InsuranceSchemeService;
import me.zhengjie.modules.insurance.service.dto.InsuranceSchemeDto;
import me.zhengjie.modules.insurance.service.dto.InsuranceSchemeQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @author Hehh
* @date 2021-12-24
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "保险方案管理")
@RequestMapping("/api/insurance-scheme")
public class InsuranceSchemeController {

    private final InsuranceSchemeService insuranceSchemeService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('insuranceScheme:list')")
    public void download(HttpServletResponse response, InsuranceSchemeQueryCriteria criteria) throws IOException {
        insuranceSchemeService.download(insuranceSchemeService.queryAll(criteria), response);
    }

    @GetMapping
    //@Log("查询保险方案")
    @ApiOperation("查询保险方案")
    @PreAuthorize("@el.check('insuranceScheme:list')")
    public ResponseEntity<Object> query(InsuranceSchemeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(insuranceSchemeService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增保险方案")
    @ApiOperation("新增保险方案")
    @PreAuthorize("@el.check('insuranceScheme:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody InsuranceScheme resources){
        return new ResponseEntity<>(insuranceSchemeService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改保险方案")
    @ApiOperation("修改保险方案")
    @PreAuthorize("@el.check('insuranceScheme:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody InsuranceScheme resources){
        insuranceSchemeService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除保险方案")
    @ApiOperation("删除保险方案")
    @PreAuthorize("@el.check('insuranceScheme:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        insuranceSchemeService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 所有
     *
     * @param criteria 标准
     * @return {@link ResponseEntity}<{@link List}<{@link InsuranceSchemeDto}>>
     */
    @PostMapping("/all")
    public ResponseEntity<List<InsuranceSchemeDto>> all(@RequestBody InsuranceSchemeQueryCriteria criteria) {
        return new ResponseEntity<>(insuranceSchemeService.queryAll(criteria), HttpStatus.OK);
    }
}